module RailsEnhancer
  class FileFormatter
    include RubyBeautify

    def initialize(file_path)
      @file_path = file_path
    end

    def format_rb_file
      content = get_file_content
      content = pretty_string content, indent_token: ' ', indent_count: 2
      write_content_to_file(content)
    end

    private

    def write_content_to_file(content)
      fh = open(@file_path, 'w')
      fh.write content
      fh.sync
      fh.close
    end

    def get_file_content
      fh = open(@file_path)
      content = fh.read
      fh.sync
      fh.close
      content
    end
  end
end